package petertel.pbx.connector;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

public class Executor {

	private static final String defaultUsername;
	private static final String password;
	private static final String hostname;

	static {
		File file = new File("pbx.properties");
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		defaultUsername = props.getProperty("username");
		password = props.getProperty("password");
		hostname = props.getProperty("hostname");
	}

	private final IConnection conn;

	private final String username;
	
	public String getUsername() {
		return username;
	}

	public Executor(String username, String password, String hostname) {
		this.username = username;
		conn = ConnectionFactory.getInstance().openConnection(username,
				password, hostname);
	}

	public Executor() {
		this(defaultUsername, password, hostname);
	}

	public IResult exec(String command) {
		return conn.execute(command);
	}

	public void close() {
		conn.disconnect();
	}

	public static void main(String[] arg) {
		Executor executor = new Executor("tel", "tel", "os4test");
		long t1 = new Date().getTime();
		long t2 = new Date().getTime();
		IResult result = executor.exec("ls -F");
		System.out.println(result);
		long t3 = new Date().getTime();
		result = executor.exec("ls -F dddddd");
		System.out.println(result);
		long t4 = new Date().getTime();
		result = executor.exec("ls /bin");
		System.out.println(result);
		long t5 = new Date().getTime();
		System.out.println(t2 - t1);
		System.out.println(t3 - t2);
		System.out.println(t4 - t3);
		System.out.println(t5 - t4);
	}
}
